package com.fbee.smartcommunities.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fbee.smartcommunities.entity.Shop;
import com.fbee.smartcommunities.mapper.ShopMapper;
import com.fbee.smartcommunities.service.ShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbee.smartcommunities.service.UploadFileService;
import io.minio.errors.*;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-27
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {

    @Resource
    private UploadFileService uploadFileService;

    @Override
    public Shop getShopInfoByUid(Long uid) {
        return this.getOne(new QueryWrapper<Shop>().eq("user_id",uid));
    }

    @Override
    public Boolean updateShopInfo(Long id, String name, String description, String address, String phone, MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        UpdateWrapper<Shop> updateWrapper = new UpdateWrapper<>();
        if(file != null ){
            String images = uploadFileService.upload("images", file);
            updateWrapper.set("logo",images);
        }
        if(name != null) updateWrapper.set("name",name);
        if(description != null) updateWrapper.set("description",description);
        if(address != null) updateWrapper.set("address",address);
        if(phone != null) updateWrapper.set("phone",phone);
        updateWrapper.eq("id",id);
        return this.update(null,updateWrapper);
    }
}
